package singleton.type1;

/**
 * 饿汉式：静态常量
 * 可能会导致内存浪费
 *
 * @author yi qiang
 * @date 2021/5/2 22:07
 */
public class Singleton1 {

    public static void main(String[] args) {
        Singleton singleton=Singleton.getInstance();
        Singleton singleton1=Singleton.getInstance();
        System.out.println(singleton==singleton1);
        System.out.println(singleton.hashCode());
        System.out.println(singleton1.hashCode());
    }


}

class Singleton{

    //1.构造器私有化,使得外部不能通过new创建实例
    private Singleton(){
    }

    //2.本类内部创建一个实例
    private  static final Singleton INSTANCE=new Singleton();

    //3.对外暴露一个公共的静态方法获取实例
    public static Singleton getInstance(){
        return INSTANCE;
    }
}
